import React, { useState } from 'react';
import { Button, Modal, message } from 'antd';
import { handlebedBatchDel } from '../api/Sickbed/sickApi'
import { getBedListDel } from '../stores/bedMassage'
import { getDrugsListDel } from '../stores/drugsManage'
import { handleBatchDrugs } from '../api/Drugs/DrugsApi'
import { useDispatch } from 'react-redux';
interface BatchDelProps {
    selectedRowKeys: React.Key[];
}
function BatchDel(props: BatchDelProps) {
    const [open, setOpen] = useState(false);
    const [confirmLoading, setConfirmLoading] = useState(false);
    const [modalText, setModalText] = useState('您正在批量删除床位，此操作不可逆，确定删除吗？');
    const [messageApi, contextHolder] = message.useMessage();
    const dispatch = useDispatch()
    const handleBatchDel = async () => {
        setOpen(true);
    }

    const handleOk = () => {
        setConfirmLoading(true);
        // setTimeout(() => {

        // }, 1000);
        console.log(props.selectedRowKeys);
        handlebedBatchDel({ ids: (localStorage as any).getItem('newSelectedRowKeys') }).then((res) => {
            props.selectedRowKeys.forEach((key) => {
                dispatch(getBedListDel(key))
            });
            if (res.code === 1) {
                messageApi.open({
                    type: 'success',
                    content: res.msg,
                });
            }else{
                messageApi.open({
                    type: 'warning',
                    content: res.msg,
                });
            }
            setOpen(false);
            setConfirmLoading(false);
        })
        handleBatchDrugs({ ids: (localStorage as any).getItem('newSelectedRowKeys') }).then((res) => {
            console.log(res);
            props.selectedRowKeys.forEach((key) => {
                dispatch(getDrugsListDel(key))
            });
            if (res.code === 1) {
                messageApi.open({
                    type: 'success',
                    content: res.msg,
                });
            }else{
                messageApi.open({
                    type: 'warning',
                    content: res.msg,
                });
            }
            setOpen(false);
            setConfirmLoading(false);
        })
    };

    const handleCancel = () => {
        console.log('Clicked cancel button');
        setOpen(false);
    };

    return (
        <div>
            {contextHolder}
            <Button onClick={handleBatchDel}>删除选中</Button>
            <Modal
                title="系统提示"
                open={open}
                onOk={handleOk}
                confirmLoading={confirmLoading}
                onCancel={handleCancel}
            >
                <p>{modalText}</p>
            </Modal>
        </div>
    );
}

export default BatchDel;